// pages/recomFriend/recomFriend.js
const sharePost = require('../../request/sharePost.js')
//获取应用实例
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    end_date: '',
    policy_type: null,
    policy_days: null,
    policyid: '',
    appointment_date: '',
    fillList: [{
      friends_name: '',
      friends_phone: '',
      friends_shq: '',
      friends_address: '',
      appointment_date: '',
      appointment_specific_time: '',
      is_show: 'false'
    }],
    appointment_specific_time: '',
    info: {},
    province: '',
    city: '',
    area: '',
    show: false,
    selectNum: null,
    items: [
      { name: 'false', value: '未确定时间', checked: 'true'}, 
      { name: 'true', value: '已确定体验时间' }
    ]
  },
  onMyevent: function(e) {
    console.log(e, '子组件传过来的值');
    let num = e.currentTarget.dataset.num
    let a = 'appointment_specific_time' + num
    this.setData({
      [a]: e.detail
    })
    this.data.fillList[num].appointment_specific_time = e.detail
    app.globalData.fillList = this.data.fillList
    this.setData({
      fillList: app.globalData.fillList
    })
  },
  /**
   * 初始化
   */
  init: function() {
    this.getPromotions()
    console.log('app.globalData.info', app.globalData.info)
  },
  /**
   * radio发生change事件
   */
  radioChange: function (e) {
    let i = e.currentTarget.dataset.id
    let is_show = "fillList[" + i + "].is_show"
    let appointment_date = "fillList[" + i + "].appointment_date"
    let appointment_specific_time = "fillList[" + i + "].appointment_specific_time"
    this.setData({
      [is_show]: e.detail.value,
      [appointment_date]: '',
      [appointment_specific_time]: ''
    })
    console.log('radio发生change事件，携带value值为：', e, i, typeof e.detail.value)
  },
  /**
   * 新增好友列表
   */
  insert: function() {
    var cb = this.data.fillList;
    cb.push({
      friends_name: '',
      friends_phone: '',
      friends_shq: '',
      friends_address: '',
      appointment_date: '',
      appointment_specific_time: '',
      is_show: 'false'
    });
    this.setData({
      fillList: cb
    });
  },
  /**
   * 删除好友列表
   */
  delBind: function() {
    var cb = this.data.fillList;
    console.log(cb);
    cb.pop(this.data.fillList.length);
    this.setData({
      fillList: cb
    });
  },
  /**
   * 获取活动列表
   */
  getPromotions: function() {
    sharePost.getPromotions({}).then(res => {
      console.log('获取活动列表', res)
      let policyid = res.data[0]._id 

      this.setData({
        policy_type: res.data[0].promotion_type,
        policy_days: res.data[0].promotion_days,
        policyid: res.data[0]._id,
      })
      let params = {
        uid: this.data.info._id,
        openid: this.data.info.openid,
        policyid: policyid
      }
      sharePost.getOverDate(params).then(res => {
        console.log('获取参加活动的结束时间', res)
        this.setData({
          end_date: res.data.end_date
        })
      })
    })
  },
  formSubmit: function(e) {
    let obj = e.detail.value
    console.log('cssssss', obj)
    for (let el in obj) {
      // console.log('电话校验', obj[el], '88:',el.indexOf('phone'),'11:', el)
      if(!obj[el]) {
        wx.showToast({
          title: '请填写完整信息',
          icon: 'none'
        })
        return
      } else if (el.indexOf('phone') > 0) {
        // console.log('电话校验', obj[el], el.indexOf('phone') === 1, el)
        if (!(/^1(3|4|5|7|8)\d{9}$/.test(obj[el]))) {
          wx.showToast({
            title: '请填写正确手机号',
            icon: 'none',
            duration: 2000
          })
          return
        }
      }
    }
    let newObj = []
    // 对象遍历
    for (let x in obj) {
      // 获取对象键数字
      let newNum = parseInt(x.replace(/[^0-9]/ig, ""));
      // 不存在 则新建一个空对象
      if (!newObj[newNum]) {
        // 直接取数字当数组下标
        newObj[newNum] = {};
      }
      // 存在则把键值填充进去
      newObj[newNum][x.replace(/\d+/g, '')] = obj[x]
    }
    //数组去空
    for (var i = 0; i < newObj.length; i++) {
      if (!newObj[i]) {
        newObj.splice(i, 1)
        i = i - 1;
      }
      newObj[i].friends_address = newObj[i].friends_shq + newObj[i].friends_address
    }
    let finalArr = newObj.map(function(val, index) {
      delete val.friends_shq
      return val
    })
    /**
     * 添加推荐好友预约表单
     */
    let params = {
      orderlist: finalArr,
      user_name: this.data.info.user_name,
      user_phone: this.data.info.user_phone,
      uid: this.data.info._id,
      openid: this.data.info.openid,
      policyid: this.data.policyid,
      policy_days: this.data.policy_days,
      policy_type: this.data.policy_type,
      end_date: this.data.end_date
    }
    console.log('@@@@%%%%%', params)
    sharePost.addOrder(params).then(res => {
      console.log('已推荐', res)
      wx.showToast({
        title: '已推荐',
      })
      //wx.navigateTo({
      /**
        * 2019-3-22
        * ch 修改
        * 关闭当前页跳转
      **/
      wx.redirectTo({
        url: '../my/my'
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      info: app.globalData.info
    })
    console.log('djfdkjfkdjfkdjfkdjfkdjfkd:', app.globalData.info)
    this.init()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    //获得changeTime组件
    this.changeTime = this.selectComponent("#changeTime")
  },
  //页面展示时，触发动画
  onShow: function () {
    const app = getApp()
    app.slideupshow(this, 'slide_up1', -100, 1)
    setTimeout(function () {
      app.slideupshow(this, 'slide_up2', -100, 1)
    }.bind(this), 200);
    setTimeout(function () {
      app.slideupshow(this, 'slide_up3', -100, 1)
    }.bind(this), 400);
    setTimeout(function () {
      app.slideupshow(this, 'slide_up4', -100, 1)
    }.bind(this), 600);
    setTimeout(function () {
      app.slideupshow(this, 'slide_up5', -100, 1)
    }.bind(this), 800);
    setTimeout(function () {
      app.slideupshow(this, 'slide_up6', -100, 1)
    }.bind(this), 1000);
    setTimeout(function () {
      app.slideupshow(this, 'slide_up7', -100, 1)
    }.bind(this), 1200);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function() {

  // },
  chooseAddress: function (e) {
    var that = this;
    let num = e.currentTarget.dataset.selectnum
    that.setData({
      show: true,
      selectNum: num
    })
  },
  sureSelectAreaListener: function (e) {
    var that = this;
    let num = that.data.selectNum
    console.log('num', num, e)
    that.data.fillList[num].friends_shq = e.detail.currentTarget.dataset.province + e.detail.currentTarget.dataset.city + e.detail.currentTarget.dataset.area
    app.globalData.fillList = this.data.fillList
    that.setData({
      show: false,
      province: e.detail.currentTarget.dataset.province,
      city: e.detail.currentTarget.dataset.city,
      area: e.detail.currentTarget.dataset.area,
      fillList: app.globalData.fillList
    })
    console.log("最终选择", this.data)
  },
  bindDateChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value, e, this.data.fillList)
    let num = e.currentTarget.dataset.pickernum
    this.data.fillList[num].appointment_date = e.detail.value
    console.log('num', num, this.data.fillList[num].appointment_date)
    app.globalData.fillList = this.data.fillList
    this.setData({
      fillList: app.globalData.fillList
    })
  }
})